<?php
/**
 * @author Tri Dung Pham
 */
class DataProvider extends Object
{
    private static $instance;

    protected $_info;
    protected $_cn;
    protected $_cached_result = array();
    protected $noq = 0; //number of query


    protected function  __construct()
    {
        global $db_config;
        global $_app_db_config;
        $this->_info = (count($_app_db_config) > 0) ? $_app_db_config : $db_config;
        $this->_connect();
    }

    public static function getInstance()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new DataProvider();
        }
        return self::$instance;
    }

    private function _connect()
    {
        $this->_cn = mysql_connect($this->_info['host'], $this->_info['user'], $this->_info['pass'], false, 196608);
        mysql_select_db($this->_info['dbname']);
        mysql_query("set names 'utf8'", $this->_cn);
    }

    public function query($sql)
    {
        $rs = mysql_query($sql);
        return $rs; //resource
    }

    public function _query($sql, $return = TRUE)
    {
        $rs = mysql_query($sql);
        if ($return)
        {
            $obj = new ResultSet($rs);
            $this->_cached_result[] = $obj;
            return $obj;
        }
    }

    public function get($tableName, $condition = NULL, $orderArr = NULL, $limit = 1000, $offset = 0)
    {
        $sql = "SELECT * FROM $tableName ";
        if (!is_null($condition))
        {
            $conStmt = " WHERE 1=1 ";
            foreach ($condition as $key => $value)
            {
                if (is_array($value))
                {
                    $conStmt .= " AND " . $key . " " . $value[0] . " " . (($value[1] <> null) ? "'$value[1]'" : "");
                }
                else
                {
                    $conStmt .= " AND " . $key . "=" . "'$value'";
                }
            }
            $sql .= $conStmt;
        }
        if (!is_null($orderArr))
        {
            $orderStmt = "ORDER BY ";
            if (is_array($orderArr))
            {
                foreach ($orderArr as $key => $value)
                {
                    $orderStmt .= "$key $value,";
                }
                $orderStmt = substr($orderStmt, 0, strlen($orderStmt) - 1);
            }
            else
            {
                $orderStmt .= "$orderArr ASC ";
            }
            $sql .= $orderStmt;
        }
        $sql .= " LIMIT $offset,$limit";
        return $this->_query($sql, true);
    }

    public function insert($tableName, $valueArr, $specifyColumn = TRUE)
    {
        $sql = "INSERT INTO $tableName";
        if ($specifyColumn)
        {
            $fStr = "(";
            $vStr = " VALUES(";
            foreach ($valueArr as $key => $value)
            {
                if (is_null($value)) continue;
                $fStr .= $key . ",";
                $vStr .= "'$value'" . ",";
            }
            $fStr = substr($fStr, 0, strlen($fStr) - 1) . ') ';
            $vStr = substr($vStr, 0, strlen($vStr) - 1) . ')';
            $sql .= $fStr . $vStr;
        }
        else
        {
            $vStr = " VALUES(";
            foreach ($valueArr as $value)
            {
                if (is_null($value)) continue;
                $vStr .= "'$value'" . ",";
            }
            $vStr = substr($vStr, 0, strlen($vStr) - 1) . ')';
            $sql .= $vStr;
        }
        $this->_query($sql, FALSE);
    }

    public function update($tableName, $updateArray, $condition = NULL)
    {
        $sql = "UPDATE $tableName SET ";
        foreach ($updateArray as $key => $value)
        {
            if (is_null($value)) continue;
            $sql .= "$key = '$value',";
        }
        $sql = substr($sql, 0, strlen($sql) - 1);
        if (!is_null($condition))
        {
            $conStmt = " WHERE 1=1 ";
            foreach ($condition as $key => $value)
            {
                if (is_null($value)) continue;
                if (is_array($value))
                {
                    $conStmt .= " AND " . $key . " " . $value[0] . " " . "'$value[1]'";
                }
                else
                {
                    $conStmt .= " AND " . $key . "=" . "'$value'";
                }
            }
            $sql .= $conStmt;
        }
        $this->_query($sql, FALSE);
    }


    public function remove($tableName, $condition = NULL)
    {
        $sql = "DELETE FROM $tableName ";
        if (!is_null($condition))
        {
            $conStmt = " WHERE 1=1 ";
            foreach ($condition as $key => $value)
            {
                if (is_array($value))
                {
                    $conStmt .= " AND " . $key . " " . $value[0] . " " . "'$value[1]'";
                }
                else
                {
                    $conStmt .= " AND " . $key . "=" . "'$value'";
                }
            }
            $sql .= $conStmt;
        }
        $this->_query($sql, FALSE);
    }

    public function inserted_id()
    {
        return mysql_insert_id();
    }
}

?>
